Skip to content

[yaml] - mongodb write normalization#38376

Open
derrickaw wants to merge 12 commits intoapache:masterfrom
derrickaw:20260505_mongodb_write
Open

[yaml] - mongodb write normalization#38376
derrickaw wants to merge 12 commits intoapache:masterfrom
derrickaw:20260505_mongodb_write

Conversation

@derrickaw
Copy link
Copy Markdown
Collaborator

  1. Write part of this - [yaml] Normalize MongoDBIO #28690
  2. Read part pending

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@derrickaw
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements a MongoDB write schema transform, enabling MongoDB writes in Beam YAML and providing a cross-language implementation. Key additions include the MongoDbWriteSchemaTransformProvider in Java, a Python wrapper in yaml_io.py, and integration test fixtures using Testcontainers. Review feedback identifies critical issues regarding shallow data conversion in both Java and Python implementations, which will cause failures for nested schemas. Additionally, a bug was found in the Java error handling logic where the error schema is unnecessarily nested, potentially causing schema mismatches.

I am having trouble creating individual review comments. Click here to see my feedback.

sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbWriteSchemaTransformProvider.java (105)

high

The error output PCollection is being assigned a nested error schema. The variable errorSchema (defined on line 81) already represents the schema for error records (containing the failed row and the error message). Calling ErrorHandling.errorSchema(errorSchema) again wraps this schema unnecessarily, which will lead to a schema mismatch or an unexpectedly nested structure in the error output.

          outputTuple.get(ERROR_TAG).setRowSchema(errorSchema);

sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbWriteSchemaTransformProvider.java (127-130)

high

The conversion from Beam Row to BSON Document is shallow. If the input Row contains nested Row objects or Iterable fields (arrays), row.getValue() will return Beam-specific types that the MongoDB Java driver cannot serialize. This will result in a CodecConfigurationException at runtime. A recursive conversion is required to transform nested Beam Rows into BSON Documents and Iterables into BSON Lists.

sdks/python/apache_beam/yaml/yaml_io.py (744-753)

medium

The row_to_dict function performs a shallow conversion. If the input PCollection contains rows with nested structures (nested rows or lists of rows), _asdict() or dict(row) will not recursively convert those nested Beam Row objects into dictionaries. This will likely cause serialization errors when the data is passed to the underlying mongodbio.WriteToMongoDB transform, as pymongo expects standard Python types. Additionally, avoid using a bare except: block; specify the exception type (e.g., except Exception:) to avoid catching system-exiting exceptions like SystemExit or KeyboardInterrupt.

@derrickaw
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a MongoDB write SchemaTransform, enabling MongoDB write operations in both Java and Python (YAML) Beam pipelines. The changes include the core Java provider and configuration, a Python wrapper, integration test fixtures, and registration of the transform in the standard expansion service. Review feedback highlights several issues in the Java BSON conversion logic where null values are currently skipped for Rows, Iterables, and Maps, which could lead to data loss or schema inconsistency. Additionally, improvements to the unit tests were suggested to explicitly verify the presence of null-valued keys in the output documents.

@derrickaw derrickaw marked this pull request as ready for review May 7, 2026 17:18
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the summary. You can try again by commenting /gemini summary.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

Assigning reviewers:

R: @shunping for label python.
R: @chamikaramj for label java.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants